use std::ffi::{OsStr, OsString};
use std::fmt::{write, Debug};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use crate::url::Loc ;
use crate::url::scheme::Scheme;



// 核心表示  统一管理路径模式和片段
#[derive(Clone, Default, Eq, Ord, PartialOrd, PartialEq)]
pub struct Url {
    loc : Loc , // 封装路径信息
    scheme : Scheme , // 封装模式信息
    frag : OsString ,
}


impl Deref for Url {
    type Target = Loc;
    fn deref(&self) -> &Self::Target {
        &self.loc
    }
}


impl Debug for Url {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.scheme {
            Scheme::Regular => write!(f , "Regular({:?})" , self.loc) ,
            Scheme::Search => write!(f , "Search({:?})" , self.loc) ,
            Scheme::SearchItem => write!(f , "SearchItem({:?})" , self.loc) ,
            Scheme::Archive => write!(f , "Archive({:?}) " , self.loc ) ,
        }
    }
}



impl From<Loc> for Url {
    fn from(loc: Loc) -> Self {
        Self{
            loc,
            ..Default::default()
        }
    }
}


impl From<PathBuf> for Url {
    fn from(path : PathBuf) -> Self {
        Loc::new(path).into()
    }
}

impl AsRef<OsStr> for Url {
    fn as_ref(&self) -> &OsStr {
        self.loc.as_os_str() 
    }
}

impl AsRef<Path> for Url {
    fn as_ref(&self) -> &Path {
        &self.loc
    }
}
